<?php

/**
* alltosun.com 短信静态方法 sms_helper.php
* ============================================================================
* 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* $Author: 吴松键 (wusj@alltosun.com) $
* $Date: 2014-9-23 上午11:01:07 $
* $Id$
*/

class sms_helper
{

    //发送短信
    public static function send_sms($mobile, $type, $content)
    {
        if(!in_array($type, sms_config::$send_sms_type)) {
            return array('msg' => 'error', 'info' => '没有定义此类型短信模板');
        }
        $ori_content = $content;
        $content = iconv('utf-8', 'gb2312//IGNORE', $content);
        $ua = sms_config::$sms_ua;
        $time = date('mdHi',time());
        $pw = MD5(sms_config::$sms_pw.'/'.$time);
        $urlencode = array(
                'ua' => $ua,
                'pw' => $pw,
                'mb' => $mobile,
                'ms' => $content,
                't'  => $time
            );
        $urlencode = http_build_query($urlencode);
        // $send_url = "http://59.151.6.148:8001/qxt/x.jsp?ua=".$ua."&pw=".$pw."&mb=".$mobile."&ms=".$content."&t=".$time;
        $send_url = "http://59.151.6.148:8001/qxt/x.jsp?".$urlencode;
        // $send_url = urlencode($send_url);
        $return_info = curl_request($send_url, 'get');
        $sms_info = array(
                'mobile'  => $mobile,
                'type'    => $type,
                'content' => $ori_content
            );
        if($return_info && strpos($return_info, '0') === 0) {
            $sms_info['return_info'] = $return_info;
            $sms_info['status']      = '5';
            $sms_arr = explode(',', $return_info);
            $sms_info['sms_id']      = $sms_arr[1];
            $sms_info['send_sum']      = $sms_arr[2];
        } else {
            $sms_info['return_info'] = $return_info;
            $sms_info['status']      = '1';
        }
        $return_id = _model('sms_status')->create($sms_info);

        return $return_id;
    }


    //发送验证短信
    public static function sendRegSms($mobile, $send_type)
    {
        //判断最新发送的验证码如果小于30S 直接返回
        $infos = auth_helper::getKvdata($mobile, $send_type);
        if($infos && $infos['times'] != ''  && $infos['times'] + 30 > time() )
        {
            return true;
        }
        $send_sms_template = sms_config::$send_sms_template;
        $send_sms_template = $send_sms_template[$send_type];
        $code=rand(111111,999999);
        $send_content = str_replace('{验证码}', $code, $send_sms_template);
        $return = sms_helper::send_sms($mobile, $send_type, $send_content);
        if($return){
            $time=time();
            $ip=Request::getClientIp();
            $info['code']=$code;
            $info['times']=$time;
            $info['mobile'] = $mobile;
            $info['res_name'] = $send_type;
            _model('mobile_code')->create($info);
            return true;
        } else {
            return false;
        }
    }

    //查询余额
    public static function sms_balance(){
        $ua = sms_config::$sms_ua;
        $pw = sms_config::$sms_pw;
        $send_url = "http://59.151.6.148:8001/qxt/balance.jsp?ua=".$ua."&pw=".$pw;
        $return_info = curl_request($send_url, 'get');
        if($return_info && strpos($return_info, '0') === 0) {
            $sms_arr = explode(',', $return_info);
            return $sms_arr[1];
        } else {
            return false;
        }
    }
}
?>